package apitest;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;




public class Apitestocr {

    static public String getResult(){
        String result = "";
        String host = "https://ocrapi-document.taobao.com";
        String path = "/ocrservice/document";
        String method = "POST";
        String appcode = "137543b5852546e7be6d4e4de55dee3e";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();

        byte[] image = null;
        try{
            Path path1 = Paths.get("D:\\java\\learning\\src\\main\\java\\apitest\\resource\\1.png");
            image = Files.readAllBytes(path1);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String base64 = Base64.getEncoder().encodeToString(image);

        String bodys = "{\"img\":\""+ base64 +"\",\"url\":\"\",\"prob\":false}";


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println(response.toString());
            //获取response的body
            result = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) {
        String host = "https://ocrapi-document.taobao.com";
        String path = "/ocrservice/document";
        String method = "POST";
        String appcode = "137543b5852546e7be6d4e4de55dee3e";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();

        byte[] image = null;
        try{
            Path path1 = Paths.get("D:\\java\\learning\\src\\main\\java\\apitest\\resource\\1.png");
            image = Files.readAllBytes(path1);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String base64 = Base64.getEncoder().encodeToString(image);

        String bodys = "{\"img\":\""+ base64 +"\",\"url\":\"\",\"prob\":false}";


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
//            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
